package com.example.demo.algorithm.lesson.basic.class2;

import java.util.Stack;

/**
 * @author liweizhi
 * @date 2020/4/27
 */
public class TwoStackImplementQueue<E> {
    Stack<E> stackPush = new Stack<>();
    Stack<E> stackPop = new Stack<>();

    public void offer(E element) {
        stackPush.push(element);
        pushToPop();
    }

    public E poll() {
        if (stackPop.empty() && stackPush.empty()) {
            throw new RuntimeException("Queue is empty!");
        }
        pushToPop();
        return stackPop.pop();
    }

    // push栈向pop栈倒入数据
    private void pushToPop() {
        if (stackPop.empty()) {
            while (!stackPush.empty()) {
                stackPop.push(stackPush.pop());
            }
        }
    }

    public static void main(String[] args) {
        TwoStackImplementQueue<Integer> queue = new TwoStackImplementQueue<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }
}
